package org.cloudplatform.common.test.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 类描述：
 * 作者：徐卫超 (cc)
 * 时间 2022/5/17 17:09
 */
public class CuratorBaseDemo {

    CuratorFramework curator;

    private static final String SERVER_PROXY = "192.168.101.22:2181";
    private static final int CONNECTION_TIMEOUT_MS = 3000;
    private static final int SESSION_TIMEOUT_MS = 1000;
    private static final String NAMESPACE = "services";

    @Before
    public void init() {
        System.setProperty("zookeeper.sasl.client", "false");
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        // 创建CuratorFramework实例
        this.curator = CuratorFrameworkFactory.builder()
                .connectString(SERVER_PROXY)
                .retryPolicy(retryPolicy)
                .connectionTimeoutMs(CONNECTION_TIMEOUT_MS)
                .sessionTimeoutMs(SESSION_TIMEOUT_MS)
                .namespace(NAMESPACE)
                .build();

        this.curator.start();
    }

    @Test
    public void createData() throws Exception {
        curator.create().withMode(CreateMode.PERSISTENT).forPath("/node1", "节点".getBytes());
    }

    @Test
    public void readData() throws Exception {
        System.out.println(new String(curator.getData().forPath("/node")));
    }

    @Test
    public void change() throws Exception {
        curator.setData()
//                .withVersion(10086) // 指定版本修改
                .forPath("/node", "修改节点".getBytes());
    }

    @Test // 检查是否存在
    public void exists() throws Exception {
        Stat stat = curator.checkExists()
                .forPath("/node4");
        if (stat == null) {
            System.out.println("节点不存在");
        }
    }


    @Test
    public void Stat() throws Exception {
        Stat stat = new Stat();
        curator.getData()
                .storingStatIn(stat)
                .forPath("/node");
    }


    @Test
    public void delNode() throws Exception {
        curator.delete()
                .guaranteed()  // 强制保证删除
                .deletingChildrenIfNeeded() // 递归删除子节点
                .forPath("/node");
    }

    @Test
    public void asyncCreate() throws Exception {
        Executor executor = Executors.newFixedThreadPool(2);
        curator.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .inBackground((curatorFramework, curatorEvent) -> {
                    System.out.println(String.format("eventType:%s,resultCode:%s", curatorEvent.getType(), curatorEvent.getResultCode()));
                }, executor)
                .forPath("path");
    }
}
